package generics;

import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
//        上界通配符
        // 定义时指定T为Fruit
        List<? extends Fruit> list;

// 实例化时类只能是T本身或它的子类
        list= new ArrayList(){{new Fruit();}}; //可以
        list= new ArrayList<Apple>(); //可以
//        list.add(new Apple())
//        list= new ArrayList<Food>();  //报错

        list = new ArrayList<Fruit>();
        list.add(new Fruit()); //报错
        list.add(null);  //可以

        list.get(0); //可以
    }
}

class Food{
    String name = "Food";
}
class Fruit extends Food{
    String name = "Fruit";
}
class Apple extends Fruit {
    String name = "Apple";
}

